package com.spring.sunflower.login;

import android.content.Intent;
import android.graphics.Color;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.spring.sunflower.MainActivity;
import com.spring.sunflower.R;
import com.spring.sunflower.base.BaseActivity;
import com.spring.sunflower.userinfo.GenderSelectActivity;
import com.spring.sunflower.widget.CodeEditText;

/**
 * Created by Kevin on 2021/5/21<br/>
 * <p>
 * Describe:<br/>
 */
public class LoginByAuthCodeActivity extends BaseActivity<LoginPresenter> implements CodeEditText.OnTextFinishListener, View.OnClickListener {
    private CodeEditText authCode;
    private String phoneNum;
    private TextView tvSecond, tvReSendSMS;
    private LinearLayout llNextStepContainer;
    private CountDownTimer countDownTimer;
    private boolean isOnTick = true;

    @Override
    public void initView() {
        authCode = findViewById(R.id.cet_authCode);
        authCode.setOnTextFinishListener(this);
        phoneNum = getIntent().getStringExtra("phoneNum");
        tvReSendSMS = findViewById(R.id.tvReSendSMS);
        tvSecond = findViewById(R.id.tvSeconds);
        llNextStepContainer = findViewById(R.id.ll_next_step_container);
        llNextStepContainer.setOnClickListener(this);
        startCountDown();

    }

    public void startCountDown() {
        isOnTick = true;
        tvSecond.setVisibility(View.VISIBLE);
        tvReSendSMS.setTextColor(getColorX(R.color.white_alpha_50));
        countDownTimer = new CountDownTimer(60000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                tvSecond.setText(millisUntilFinished / 1000 + "s");
            }

            @Override
            public void onFinish() {
                isOnTick = false;
                tvSecond.setVisibility(View.GONE);
                tvReSendSMS.setTextColor(Color.WHITE);
            }
        };
        countDownTimer.start();
    }

    @Override
    public int getLayoutID() {
        return R.layout.activity_login_by_auth_code;
    }

    @Override
    public LoginPresenter setPresenter() {
        return new LoginPresenter(this);
    }

    @Override
    public void onTextFinish(CharSequence text, int length) {
        mPresenter.loginByAuthCode(phoneNum, text.toString());
    }

//    @Override
//    public void requestFinish() {
//        super.requestFinish();
//        startActivity(new Intent(LoginByAuthCodeActivity.this,MainActivity.class));
//    }

    @Override
    public void requestFinishWithSth(String type) {
        super.requestFinishWithSth(type);
        if ("0".equals(type)) {
            startActivity(new Intent(LoginByAuthCodeActivity.this, GenderSelectActivity.class));
        } else {
            startActivity(new Intent(LoginByAuthCodeActivity.this, MainActivity.class));
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (countDownTimer != null) {
            countDownTimer.cancel();
            countDownTimer = null;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_next_step_container:
                if (!isOnTick) {
                    mPresenter.requestAuthCode(phoneNum,"0");
                    startCountDown();
                }
                break;
        }
    }
}
